#include "stdafx.h"
#include "CharacterRecognition.h"

//////////////////////////////////////////////////////////////////////////
// class Character
Character::Character(int pos, std::string c) : 
	m_Position(pos), m_Character(c)
{
}

bool Character::CompareFunction(Character x, Character y)
{
	return x.m_Position < y.m_Position;
}

//////////////////////////////////////////////////////////////////////////
// class CharacterRecognition
CharacterRecognition::CharacterRecognition(Mask *Image, CharacterPatternSet *PS)
{
	m_Image = Image;
	m_PS = PS;
}

CharacterRecognition::~CharacterRecognition(void)
{
}

std::string CharacterRecognition::Run(int r0/*=0*/, int r1/*=0*/, int c0/*=0*/, int c1/*=0*/)
{
	_ASSERTE( r0 >= 0 );
	_ASSERTE( c0 >= 0 );
	_ASSERTE( r1 <= m_Image->GetHeight() );
	_ASSERTE( c1 <= m_Image->GetWidth() );

	r1 == 0 ? m_Image->GetHeight()		: r1;
	c1 == 0 ? m_Image->GetWidth()		: c1;	
	
	std::string str;
	std::vector<Character> vString;

	for(int i=0; i<m_PS->Count(); i++)
	{
		std::vector<int> pos;
		std::string c = m_PS->Get(i)->GetCharacter();

		m_Image->Find(m_PS->Get(i), &pos, Mask::ReturnCol, r0, r1, c0, c1);

		for(int j=0; j<(int)pos.size(); j++)
			vString.push_back(Character(pos[j], c));

		
	}
	std::sort(vString.begin(), vString.end(), Character::CompareFunction);
	for(int i=0; i<(int)vString.size(); i++)
		str += vString[i].GetCharacter();
	return str;
}